package linuxbox.bookmark.servlet;

import javax.servlet.http.*;
import org.apache.log4j.Logger;

import linuxbox.exception.*;
import linuxbox.common.*;
import linuxbox.bookmark.logic.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class BookmarkAction extends Action
{
  protected static Logger logger = Logger.getLogger(BookmarkAction.class.getName());

  protected static String listJSP = null;
  protected static String editJSP = null;

  static BookmarkHome home = BookmarkHome.getInstance();

  public void doAction(HttpSession session, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse resp) throws Exception
  {
    String str = (String) req.getParameter("VIEW_TYPE");
    if ( str != null && str.length() > 0 )
      session.setAttribute("bookmark.VIEW_TYPE", str);

    str = (String) req.getParameter("SORTBY");
    if ( str != null && str.length() > 0 )
      session.setAttribute("bookmark.SORTBY", str);

    str = (String) req.getParameter("ASC");
    if ( str != null && str.length() > 0 )
      session.setAttribute("bookmark.ASC", str);

    str = req.getParameter("SubmitCancel");
    if ( str != null )
    {
      sendFile(listJSP,req,resp);
      return;
    }

    int curEntryID = 0;
    str = req.getParameter("ID");
    if ( str != null)
    {
      curEntryID = Integer.parseInt(str);
    }

    str = req.getParameter("SubmitSave");
    if ( str != null )
    {
      saveToBean(curEntryID, req, aBoxSession, false);
      sendFile(listJSP,req,resp);
      return;
    }

    str = req.getParameter("SubmitSysSave");
    if ( str != null )
    {
      saveToBean(curEntryID, req, aBoxSession, true);
      sendFile(listJSP,req,resp);
      return;
    }

    str = req.getParameter("SubmitUpdate");
    if ( str != null )
    {
      saveToBean(curEntryID, req, aBoxSession, false);
      sendFile(listJSP,req,resp);
      return;
    }

    str = req.getParameter("SubmitDelete");
    if ( str != null )
    {
      deleteBean(aBoxSession, curEntryID);
      sendFile(listJSP,req,resp);
      return;
    }

    str = req.getParameter("SubmitEdit");
    if ( str != null )
    {
      // Now we'll go to the edit form
      if ( curEntryID == 0 )
      {
        setEntryDefaultAttributes(req);
      }
      else
      {
        setEntryAttributes(req, curEntryID);
      }
      sendFile(editJSP,req,resp);
    }
    else
      sendFile(listJSP,req,resp);
  }

  protected void saveToBean(int id, HttpServletRequest req, BoxSession session, boolean isSystem) throws Exception
  {
    String strUrl = req.getParameter("URL");
    String strName = req.getParameter("NAME");

    if ((strUrl==null || strUrl.length()==0)
        && (strName==null || strName.length()==0))
      return;

    BookmarkEntry entry = null;
    if ( id == 0 )
      entry = home.add();
    else
      entry = home.get(id);

    if (!isSystem)
    {
      String str = req.getParameter("SYSTEM");
      if ( str != null && str.length()>0 )
      {
        int i = Integer.parseInt(str);
        isSystem = (i>0);
      }
    }

    if (isSystem)
    {
      if ( !session.isBookmarkAdmin() )
      {
        throw new AccessRightException(session.getLoginID(),"bookmark",
                                       "BookmarkAction", "", "Can't save the bookmark");
      }
      entry.setUserid(-1);
    }
    else
      entry.setUserid(session.getID());

    if (strUrl==null || strUrl.length()==0)
      strUrl = strName;
    if (strName==null || strName.length()==0)
      strName = strUrl;

    entry.setUrl(strUrl);
    entry.setName(strName);

    home.update(entry);
  }

  protected void deleteBean(BoxSession session, int id) throws Exception
  {
    if ( id != 0 )
    {
      BookmarkEntry entry = home.get(id);
      if ( (entry.getUserid()== -1 && !session.isBookmarkAdmin() )
        || (entry.getUserid()!= -1 && entry.getUserid() != session.getID()))
      {
        throw new AccessRightException(session.getLoginID(),"bookmark",
                                       "BookmarkAction", "", "Can't delete the bookmark");
      }
      home.remove(id);
    }
  }

  protected void setEntryAttributes(HttpServletRequest req, int ID)
  {
    BookmarkEntry entry = home.get(ID);

    //Set the defalut attributes for a new entry
    req.setAttribute("ID", String.valueOf(entry.getID()));
    req.setAttribute("NAME", entry.getName());
    req.setAttribute("URL", entry.getUrl());
    if (entry.getUserid() == -1)
      req.setAttribute("SYSTEM", "1");
  }

  protected void setEntryDefaultAttributes(HttpServletRequest req)
  {
    //Set the defalut attributes for a new entry
    req.setAttribute("NAME", "");
    req.setAttribute("URL", "http://");
  }

  public void setListJSP(String str)
  {
    listJSP = str;
  }
  public void setEditJSP(String str)
  {
    editJSP = str;
  }
}
